#ifndef __URI_H__
#define __URI_H__

#include <string>
#include <memory>
#include <stdint.h>
#include "address.h"

/*
     foo://user@sylar.com:8042/over/there?name=ferret#nose
       \_/   \______________/\_________/ \_________/ \__/
        |           |            |            |        |
     scheme     authority       path        query   fragment
*/

class Uri
{
public:
     typedef std::shared_ptr<Uri> ptr;

     static Uri::ptr Create(const std::string &uri);

     Uri();

     const std::string &getScheme() const { return m_scheme; }
     const std::string &getUserinfo() const { return m_userinfo; }
     const std::string &getHost() const { return m_host; }
     const std::string &getPath() const;
     const std::string &getQuery() const { return m_query; }
     const std::string &getFragment() const { return m_fragment; }
     int32_t getPort() const;
     
     void setScheme(const std::string &scheme) { m_scheme = scheme; }
     void setUserinfo(const std::string &userinfo) { m_userinfo = userinfo; }
     void setHost(const std::string &host) { m_host = host; }
     void setPath(const std::string &path) { m_path = path; }
     void setQuery(const std::string query) { m_query = query; }
     void setFragment(const std::string &fragment) { m_fragment = fragment; }
     void setPort(const int32_t &port) { m_port = port; }

     // 序列化到输出流
     std::ostream &dump(std::string &os) const;

     // 转换成字符串
     std::string toString() const;

     // 获取Address
     Address::ptr createAddress() const;

private:
     // 是否默认端口
     bool isDefaultPort() const;

private:
     // schema
     std::string m_scheme;
     // 用户信息
     std::string m_userinfo;
     // host
     std::string m_host;
     // 路径
     std::string m_path;
     // 查询参数
     std::string m_query;
     // fragment
     std::string m_fragment;
     // 端口
     int32_t m_port;
};

#endif
